/**
 * @class AkData.controller.Main
 * @extends Ext.app.Controller
 *
 * This is an abstract base class that is extended by both the phone and tablet versions. This controller is
 * never directly instantiated, it just provides a set of common functionality that the phone and tablet
 * subclasses both extend.
 */
Ext.define('AkData.controller.Main', {
    extend: 'Ext.app.Controller',

    config: {
        /**
         * @private
         */
        viewCache: [],

        refs: {
            nav: '#mainNestedList',
            main: 'mainview',
            toolbar: '#mainNavigationBar',
            sourceButton: 'button[action=viewSource]',
            //themeToggleButton: 'button[action=toggleTheme]',

			/*
            sourceOverlay: {
                selector: 'sourceoverlay',
                xtype: 'sourceoverlay',
                autoCreate: true
            }
			*/
        },

        control: {
            sourceButton: {
                tap: 'onSourceTap'
            },
			/*
            themeToggleButton: {
                tap: 'onThemeToggleTap'
            },
			*/
            nav: {
                itemtap: 'onNavTap'
            }
        },
		
        routes: {
            //'demo/:id': 'showViewById',
			'ak/:id': 'showViewById',
            'menu/:id': 'showMenuById',
            '': 'showMenuById'
        },

        /**
         * @cfg {Ext.data.Model} currentDemo The Ak that is currently loaded. This is set whenever showViewById
         * is called and used by functions like onSourceTap to fetch the source code for the current ak.
         */
        currentDemo: undefined
    },

    /**
     * Finds a given view by ID and shows it. End-point of the "ak/:id" route
     */
    showViewById: function (id) {
        var nav = this.getNav(),
            view = nav.getStore().getNodeById(id);
		
        this.showView(view);
        //this.setCurrentDemo(view);
        //this.hideSheets();
		//for (i = 0; i < 100; i++) window.location.replace('http://localhost/mornek2/#' + i);
		
		//this.getApplication().getHistory().add(Ext.create('Ext.app.Action', {url: 'ak/'}));
		//this.getApplication().getHistory().add(Ext.create('Ext.app.Action', {url: 'ak/' + window.location}));
		
    },

    /**
     * Shows the source code for the {@link #currentDemo} in an overlay
     */
	
    onSourceTap: function () {
        window.location.assign("php/gc.php?cikis=1");
    },
	/*
    onThemeToggleTap: function() {
        switch(Ext.theme.name) {
            case "Tizen": {
                if (!AkData.app.getThemeVariationTransitionCls()) {
                    AkData.app.setThemeVariationTransitionCls("tizenThemeTransition");
                }

                if (AkData.app.getThemeVariation() === "light") {
                    AkData.app.setThemeVariation("dark");
                } else {
                    AkData.app.setThemeVariation("light");
                }
            }
        }

    },
	*/
    /**
     * @private
     * In the kitchen sink we have a large number of dynamic views. If we were to keep all of them rendered
     * we'd risk causing the browser to run out of memory, especially on older devices. If we destroy them as
     * soon as we're done with them, the app can appear sluggish. Instead, we keep a small number of rendered
     * views in a viewCache so that we can easily reuse recently used views while destroying those we haven't
     * used in a while.
     * @param {String} name The full class name of the view to create (e.g. "AkData.view.Forms")
     * @return {Ext.Component} The component, which may be from the cache
     */
    createView: function (item) {
        var name = this.getViewName(item),
            cache = this.getViewCache(),
            ln = cache.length,
            limit = item.get('limit') || 20,
            view, i = 0, j, oldView;

        for (; i < ln; i++) {
            if (cache[i].viewName === name) {
                return cache[i];
            }
        }

        if (ln >= limit) {
            for (i = 0, j = 0; i < ln; i++) {
                oldView = cache[i];
                if (!oldView.isPainted()) {
                    oldView.destroy();
                } else {
                    cache[j++] = oldView;
                }
            }
            cache.length = j;
        }

        view = Ext.create(name);
        view.viewName = name;
        cache.push(view);
        this.setViewCache(cache);

        return view;
    },

    /**
     * @private
     * Returns the full class name of the view to construct for a given Ak
     * @param {AkData.model.Ak} item The Ak
     * @return {String} The full class name of the view
     */
    getViewName: function (item) {
        var name = item.get('view') || item.get('text'),
            ns = 'AkData.view.';
		
		/*
        if (name == 'TouchEvents') {
            if (this.getApplication().getCurrentProfile().getName() === 'Tablet') {
                return ns + 'tablet.' + name;
            } else {
                return ns + 'phone.' + name;
            }
        } else {
            return ns + name;
        }
		*/
		return ns + name;
    },

    /**
     * we iterate over all of the floating sheet components and make sure they're hidden when we
     * navigate to a new view. This stops things like Picker overlays staying visible when you hit
     * the browser's back button
     */
    hideSheets: function () {
        Ext.each(Ext.ComponentQuery.query('sheet, #editorPanel'), function (sheet) {
            if(sheet instanceof Ext.Menu) {
                Ext.Viewport.hideMenu(sheet);
            }else {
                sheet.setHidden(true);
            }
        });
    }
});
